﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MySql授权小工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string method = "";
            if (checkBox1.Checked)
            {
                method = "SELECT, DELETE, UPDATE, INSERT";
                if (checkBox3.Checked)
                {
                    method = "SELECT, DELETE, UPDATE, INSERT,CREATE,ALTER";
                }
            }
            else
            {
                method = "all";
            }

            //string[] ip = new string[textBox1.Lines.Length];
            for (int i = 0; i < textBox1.Lines.Length; i++)
            {
                string ip = textBox1.Lines[i];
                string sql2 = ""; string sql3 = "";

                string sql1 = "GRANT " + method + " on " + textBox4.Text + ".* TO " + textBox2.Text + "@" + ip;
                if (checkBox2.Checked)
                {
                    sql2 = "GRANT ALL  on mysql.proc to " + textBox2.Text + "@" + ip;
                    sql3 = "GRANT create routine, alter routine, execute  on " + textBox4.Text + ".* to " + textBox2.Text + "@" + ip;
                }
                string sql4 = "flush privileges;";
                if (!string.IsNullOrEmpty(textBox3.Text))
                {
                    sql1 = sql1 + " IDENTIFIED BY  '" + textBox3.Text + "';";
                    if (checkBox2.Checked)
                    {
                        sql2 = sql2 + " IDENTIFIED BY  '" + textBox3.Text + "';";
                        sql3 = sql3 + " IDENTIFIED BY  '" + textBox3.Text + "';";
                    }
                }
                else
                {
                    sql1 = sql1 + ";";
                    if (checkBox2.Checked)
                    {
                        sql2 = sql2 + ";";
                        sql3 = sql3 + ";";
                    }
                }
                textBox5.AppendText("\r\n\r\n-- 生成的sql\r\n" + sql1 + "\r\n" + sql2 + "\r\n" + sql3 + "\r\n" + sql4 + "\r\n\n\n");
            }
            //保存连接数据的信息
            textBox6.AppendText(
                    "[库名]" + textBox4.Text + "\t" + "[账号]" + textBox2.Text + "\t" + "[密码]" + textBox3.Text + "\t" + "[ip]" + "\r\n"
                );
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox5.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox3.Text = this.GetRandomString(20);
        }

        private static int getNewSeed()
        {
            byte[] rndBytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(rndBytes);
            return BitConverter.ToInt32(rndBytes, 0);

        }

        public string GetRandomString(int len)
        {
            string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ@$%&*";
            string reValue = string.Empty;
            Random rnd = new Random(getNewSeed());
            while (reValue.Length < len)
            {
                string s1 = s[rnd.Next(0, s.Length)].ToString();
                if (reValue.IndexOf(s1) == -1) reValue += s1;
            }
            return reValue;
        }
    }

}
